﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Store.Model.Models;
using Store.Bll;
using Store.Dal;
using Store.Helper;
namespace Store.Server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Store.Server", Version = "v1" });
            });
            //依赖注入数据库
            services.AddDbContext<StoreContext>(option => option.UseSqlServer("Data Source=GAOLEI\\SQLEXPRESS;Initial Catalog=Store;Integrated Security=True"));
            //依赖注入上下文
            services.AddTransient<StoreContext>();
            //依赖注入登录
            services.AddTransient<EmployeeRegisterDal>();
            services.AddTransient<EmployeeRegisterBll>();
            //依赖注入订单
            services.AddTransient<HandlingOrderDal>();
            services.AddTransient<HandlingOrderBll>();
            //依赖注入用户
            services.AddTransient<UsersDal>();
            services.AddTransient<UsersBll>();
            //依赖注入促销
            services.AddTransient<PromotionDAL>();
            services.AddTransient<PromotionBLL>();
            //依赖注入商品
            services.AddTransient<CommodityDAL>();
            services.AddTransient<CommunityBll>();
            //依赖注入商品分类
            services.AddTransient<TypeeDAL>();
            services.AddTransient<TypeeBll>();
            //shouye
            services.AddTransient<OnePageDAL>();
            services.AddTransient<onePageBLL>();

            //redis缓存
            var section = Configuration.GetSection("Redis:Default");
            //连接字符串
            string _connectionString = section.GetSection("Connection").Value;
            //实例名称
            string _instanceName = section.GetSection("InstanceName").Value;
            //默认数据库 
            int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
            services.AddSingleton(new RedisHelper(_connectionString, _instanceName, _defaultDB));
            //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            //配置跨域
            //允许一个或多个来源可以跨域
            services.AddCors(options =>
            {
                options.AddPolicy("any", policy =>
                {
                    // 设定允许跨域的来源，有多个可以用','隔开
                    policy.WithOrigins("http://localhost:64754")//只允许https://localhost:5000来源允许跨域
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
                });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Store.Server v1"));
            }

            app.UseRouting();

            //����Cors
            app.UseCors("any");

            //app.UseMvc();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
